package com.mywork.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.MyFieldWorkBean;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.Req.GetMyGuide;
import com.ciko.code.type.EmployeeEnum;
import com.mywork.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by LEECO on 2016/6/2.
 */
public class MyFieldWorkDetailActivity extends BaseActicity {

    private RelativeLayout myFieldWordDetailWorker;
    private TextView myFieldWordDetailWorkerRea,myFieldWordDetailWorkerName,myFieldWordDetailUserZhiWei,
            tvMyFieldWorkDetaCusName,tvMyFieldWorkDetaTime,tvMyFieldWorkDetaAddress,tvMyFieldWorkDetaReason,
            tVFeedBackedTxt,tvMyFieldWorkDetaBossAdvice,tvMyFieldWorkDetaBossHeadPic,
            ivMyFieldWorkDetaBossGuide2Headpic;
    private LinearLayout lLFeedBacked,llMyFieldWorkDetaBossGuide1,llMyFieldWorkDetaBossGuide2,lLMyFieldWorkReason;
    private EditText eTMyFieldWorkDetaBossGuideInput;
    private Button btnToInputFeedBack;
    private ImageView myFieldWordDetailWorkerHeadpic;


    private int type=-1;//根据传递的type值，判断是我发送的外勤，还是我收到的外勤
    private MyFieldWorkBean fb;
    private boolean isFeedBack;//是否已经反馈
    private boolean isGuide;//领导是否已经指导
    private final int SEND=1;//发出的签到
    private final int RECEIVE=2;//收到的签到
    private String feedback;

    @Override
    protected void initTopBar() {
        setTitle("签到详情");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_myfieldwork_detail);

        type = this.getIntent().getIntExtra("type",-1);
        fb = (MyFieldWorkBean) getIntent().getSerializableExtra("bean");fb = (MyFieldWorkBean) getIntent().getSerializableExtra("bean");
        if(!isEmpty(fb.getFeedback().trim())) isFeedBack=true;
        else isFeedBack=false;

        if (!isEmpty(fb.getDirection().trim())) isGuide = true;
        else isGuide=false;
    }

    @Override
    protected void initView() {
        myFieldWordDetailWorker = (RelativeLayout) findViewById(R.id.myFieldWordDetailWorker);
        myFieldWordDetailWorkerHeadpic = (ImageView) findViewById(R.id.myFieldWordDetailWorkerHeadpic);
        tvMyFieldWorkDetaBossHeadPic = (TextView) findViewById(R.id.tvMyFieldWorkDetaBossHeadPic);
        ivMyFieldWorkDetaBossGuide2Headpic = (TextView) findViewById(R.id.ivMyFieldWorkDetaBossGuide2Headpic);

        myFieldWordDetailWorkerRea = (TextView) findViewById(R.id.myFieldWordDetailWorkerRea);
        myFieldWordDetailWorkerName = (TextView) findViewById(R.id.myFieldWordDetailWorkerName);
        myFieldWordDetailUserZhiWei = (TextView) findViewById(R.id.myFieldWordDetailUserZhiWei);
        tvMyFieldWorkDetaCusName = (TextView) findViewById(R.id.tvMyFieldWorkDetaCusName);
        tvMyFieldWorkDetaTime = (TextView) findViewById(R.id.tvMyFieldWorkDetaTime);
        tvMyFieldWorkDetaAddress = (TextView) findViewById(R.id.tvMyFieldWorkDetaAddress);
        tvMyFieldWorkDetaReason = (TextView) findViewById(R.id.tvMyFieldWorkDetaReason);
        tVFeedBackedTxt = (TextView) findViewById(R.id.tVFeedBackedTxt);
        tvMyFieldWorkDetaBossAdvice = (TextView) findViewById(R.id.tvMyFieldWorkDetaBossAdvice);


        lLFeedBacked = (LinearLayout) findViewById(R.id.lLFeedBacked);
        lLMyFieldWorkReason = (LinearLayout) findViewById(R.id.lLMyFieldWorkReason);
        lLMyFieldWorkReason.setVisibility(View.VISIBLE);
        llMyFieldWorkDetaBossGuide1 = (LinearLayout) findViewById(R.id.llMyFieldWorkDetaBossGuide1);
        llMyFieldWorkDetaBossGuide2 = (LinearLayout) findViewById(R.id.llMyFieldWorkDetaBossGuide2);

        eTMyFieldWorkDetaBossGuideInput = (EditText) findViewById(R.id.eTMyFieldWorkDetaBossGuideInput);
        btnToInputFeedBack = (Button) findViewById(R.id.btnToInputFeedBack);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        tvMyFieldWorkDetaCusName.setText(Html.fromHtml(
                "<font color='#2b2b2b'>"+fb.getCustName()+"</font>"));
        tvMyFieldWorkDetaTime.setText(Html.fromHtml("<font color='#8c8c8c'>拜访时间：</font>"+
                "<font color='#2b2b2b'>"+convertDate(fb.getVisitTime())+"</font>"));
        tvMyFieldWorkDetaAddress.setText(Html.fromHtml("<font color='#2b2b2b'>"+fb.getCustAddr()+"</font>"));
        tvMyFieldWorkDetaReason.setText(Html.fromHtml(
                "<font color='#2b2b2b'>"+fb.getVisitCause()+"</font>"));

        if (type==SEND&&isFeedBack==false){//我的签到&&没有反馈
            btnToInputFeedBack.setVisibility(View.VISIBLE);
            btnToInputFeedBack.setText("拜访反馈");
            btnToInputFeedBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MyFieldWorkDetailActivity.this,FeedBackActivity.class);
                    intent.putExtra("outsideId",fb.getId());
                    startActivityForResult(intent,100);
                }
            });
        }else if(type==SEND&&isFeedBack==true){//我的签到&&已经反馈
            btnToInputFeedBack.setVisibility(View.GONE);
            lLFeedBacked.setVisibility(View.VISIBLE);
            tVFeedBackedTxt.setText(fb.getFeedback());
        }
        if (isGuide){
            llMyFieldWorkDetaBossGuide1.setVisibility(View.VISIBLE);
            tvMyFieldWorkDetaBossHeadPic.setBackgroundResource(R.drawable.shape_round_green);
            tvMyFieldWorkDetaBossHeadPic.setText(fb.getLeaderName());
            tvMyFieldWorkDetaBossAdvice.setText(fb.getDirection());
        }else{
            llMyFieldWorkDetaBossGuide1.setVisibility(View.GONE);
        }

        if (type==RECEIVE){
            myFieldWordDetailWorker.setVisibility(View.VISIBLE);
//            myFieldWordDetailWorkerHeadpic.setBackgroundResource(R.drawable.shape_round_green);
//            myFieldWordDetailWorkerHeadpic.setText(fb.getStaffName());
            if (!TextUtils.isEmpty(fb.getUserHead())) {
                Picasso.with(MyFieldWorkDetailActivity.this)
                        .load(fb.getUserHead())
                        .transform(new CircleTransform())
                        .placeholder(R.drawable.user_placeholder)
                        .error(R.drawable.user_placeholder)
                        .into(myFieldWordDetailWorkerHeadpic);
            }else{
                Picasso.with(MyFieldWorkDetailActivity.this)
                        .load(R.drawable.user_placeholder)
                        .transform(new CircleTransform())
                        .into(myFieldWordDetailWorkerHeadpic);
            }
            myFieldWordDetailWorkerRea.setText("外勤签到");
            myFieldWordDetailWorkerName.setText(fb.getStaffName());
            myFieldWordDetailUserZhiWei.setText(EmployeeEnum.typeOfValue(fb.getUserPositionMark()));
            if (isFeedBack){
                lLFeedBacked.setVisibility(View.VISIBLE);
                tVFeedBackedTxt.setText(fb.getFeedback());
            }else{
                lLFeedBacked.setVisibility(View.GONE);
            }
            if (!isGuide){
                llMyFieldWorkDetaBossGuide2.setVisibility(View.VISIBLE);
                ivMyFieldWorkDetaBossGuide2Headpic.setBackgroundResource(R.drawable.shape_round_green);
                ivMyFieldWorkDetaBossGuide2Headpic.setText(fb.getLeaderName());
                btnToInputFeedBack.setText("确定");
                btnToInputFeedBack.setVisibility(View.VISIBLE);
                btnToInputFeedBack.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(eTMyFieldWorkDetaBossGuideInput.getText().toString().trim())){
                            doHttpRequst(fb.getId(),eTMyFieldWorkDetaBossGuideInput.getText().toString());
                        }else{
                            Toast.makeText(MyFieldWorkDetailActivity.this,"请输入领导指导",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                });
            }
        }
    }
    private void doHttpRequst(String outsideId, String direction) {
        HttpClient.getInstance(this).post(new GetMyGuide(outsideId,direction), this);
    }

    @Override
    public void onRequestStart(String tag) {
        super.onRequestStart(tag);
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        llMyFieldWorkDetaBossGuide2.setVisibility(View.GONE);
        llMyFieldWorkDetaBossGuide1.setVisibility(View.VISIBLE);
        tvMyFieldWorkDetaBossHeadPic.setBackgroundResource(R.drawable.shape_round_green);
        tvMyFieldWorkDetaBossHeadPic.setText(fb.getLeaderName());
        tvMyFieldWorkDetaBossAdvice.setText(eTMyFieldWorkDetaBossGuideInput.getText().toString());
        btnToInputFeedBack.setVisibility(View.GONE);
        Toast.makeText(MyFieldWorkDetailActivity.this,"领导指导提交成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        Toast.makeText(MyFieldWorkDetailActivity.this,"提交失败，请稍后重试",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==100&&resultCode==200){
            feedback = data.getStringExtra("feedback");
            lLFeedBacked.setVisibility(View.VISIBLE);
            btnToInputFeedBack.setVisibility(View.GONE);
            tVFeedBackedTxt.setText(feedback);
        }
    }

    @Override
    protected void initListener() {

    }

    public String convertDate(long timeT){
        SimpleDateFormat df3 = new SimpleDateFormat("MM月dd日 HH:mm");
        String date = df3.format(new Date( timeT));
        return date ;
    }

    public boolean isEmpty(String str){
        if (null==str||str.equals("null")||str.length()==0) return true;
        else return false;
    }


    public class CircleTransform implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());

            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                source.recycle();
            }

            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap,
                    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);

            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);

            squaredBitmap.recycle();
            return bitmap;
        }

        @Override
        public String key() {
            return "circle";
        }
    }
}
